const _ = require('lodash');
const async = require('async');
const commonServices = require('../common');
const enums = require('../../enums');

function manpowerRequisitionCreateHiringManager(data, callback) {
  const models = commonServices.getModels();
  const { WfRunInstance: formDetailModel } = models;
  const formDetail = data.formDetail;
  if (data.formStatus != enums.formStatus.submitted) return callback(null, {});
  const createHiringManager = (next) => {
    commonServices.createSystemUser(formDetail.hiringManager, next);
  };
  const updateHiringManager = (hiringManager, next) => {
    formDetailModel.upsert({
      id: data.id,
      formDetail: _.assign(formDetail, { hiringManager }),
    }, next);
  };
  async.waterfall([createHiringManager, updateHiringManager], callback);
}

function manpowerRequisitionCreateBusiness(data, callback) {
  const models = commonServices.getModels();
  const { SysUserRoleMapping: userRoleMappingModel, WfRunInstance: formDetailModel, WfRunFlowDetail: approvalDetailModel } = models;
  const formDetail = data.formDetail;
  let business = {};
  let isExist = true;
  if (data.formStatus != enums.formStatus.approved1) return callback(null, {});
  if (_.isEmpty(formDetail.business)) return callback(null, {});
  const createBusiness = (next) => {
    formDetail.business.others = { formIds: [2] };
    commonServices.createSystemUser(formDetail.business, next);
  };
  const getBusinessRole = (user, next) => {
    commonServices.getCount(userRoleMappingModel, { employeeId: user.employeeId, roleId: 11 }, (error, count) => {
      isExist = count != 0;
      business = user;
      next(error);
    });
  };
  const updateBusinessRole = (next) => {
    if (isExist) return next(null);
    userRoleMappingModel.create({ userEmployeeId: business.employeeId, roleId: 11, others: { formIds: [2] } }, error => next(error));
  };
  const updateFormDetailBusiness = (next) => {
    formDetailModel.upsert({
      id: data.id,
      formDetail: _.assign(formDetail, { business }),
    }, error => next(error));
  };
  const deleteApprovalDetail = (next) => {
    approvalDetailModel.destroyAll({
      instanceId: data.id,
      approvalStep: 2,
    }, error => next(error));
  };
  const createApprovalDetail = (next) => {
    approvalDetailModel.upsert({
      formId: data.form().id,
      flowId: data.flow().id,
      stepId: 2,
      instanceId: data.id,
      approvalStatus: enums.approvalStatus.pending,
      approvalStep: 2,
      prevApprovalStep: 1,
      nextApprovalStep: 3,
      requesterEmployeeId: data.sys_owner,
      approverEmployeeId: business.employeeId,
      approvalName: business.chineseName,
      approvalEnglishName: business.englishName,
      comment: '',
    }, error => next(error));
  };
  async.waterfall([createBusiness, getBusinessRole, updateBusinessRole, updateFormDetailBusiness, deleteApprovalDetail, createApprovalDetail], callback);
}

// function manpowerRequisitionCreatePrUsers(data, callback) {
//   const formDetail = data.formDetail;
//   const createATSApprover = (next) => {
//     commonServices.createSystemUser(formDetail.atsApprover, next);
//   };
//   const createOpOwner = (ATSApprover, next) => {
//     commonServices.createSystemUser(formDetail.opOwner, next);
//   };
//   async.waterfall([createATSApprover, createOpOwner], callback);
// }

module.exports = {
  manpowerRequisitionCreateHiringManager,
  manpowerRequisitionCreateBusiness,
  // manpowerRequisitionCreatePrUsers,
};
